package Test;

import domain.Book;


import java.util.ArrayList;
import java.util.Scanner;

public class BookManage {
    public static void main(String[] args) {
        ArrayList<Book> list = new ArrayList<>();
     loo:   while (true) {
         System.out.println("------欢迎进入图书馆管理系统------");
         System.out.println(" 1、添加图书 2、查询图书 3、查询单本图书信息 4、删除图书 5、修改图书的信息 6、退出");
         Scanner sc = new Scanner(System.in);
         int num = sc.nextInt();
            switch (num) {
                case 1:
                    System.out.println("---欢迎添加书籍---");
                    addBook(list);
                    break ;
                case 2:
                    System.out.println("---欢迎进入查询页面---");
                    lookBook(list);
                    break;
                case 3:
                    System.out.println("---欢迎进入查询单本书籍页面---");
                    lookDanBook(list);
                    break;
                case 4:
                    System.out.println("---欢迎进入删除页面---");
                    deleteBook(list);
                    break;
                case 5:
                    System.out.println("---欢迎进入修改页面---");
                    setBook(list);
                    break;
                case 6:
                    System.out.println("---欢迎下次使用---");
                    break loo;
                default:
                    System.out.println("你输入数据有误");
                   break ;
            }
        }
    }

    private static void setBook(ArrayList<Book> list) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入你要修改的书目的ID");
            int oldID = sc.nextInt();
            for (int i = 0; i < list.size(); i++) {
                if(oldID==list.get(i).getId()){
                    String oldName = sc.next();
                    if(oldName.equals(list.get(i).getName())){
                        System.out.println("请输入你要新的书名");
                        String name = sc.next();
                        list.get(i).setName(name);
                        System.out.println("请输入你要新的作者");
                        String author = sc.next();
                        list.get(i).setAuthor(author);
                        System.out.println("请输入你要新的价钱");
                        double price = sc.nextDouble();
                        list.get(i).setPrice(price);
                        System.out.println("修改成功");
                    }else{
                        System.out.println("查无此书，请重新输入数目的ID");
                    }
                }else {
                    System.out.println("查无此书，请重新输入数目的ID");
                }
            }
        }
    }

    private static void deleteBook(ArrayList<Book> list) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入你要删除书的ID");
            int oldId = sc.nextInt();
            System.out.println("请输入你要删除书的名字");
            String name = sc.next();
            for (int i = 0; i < list.size(); i++) {
                if (oldId == list.get(i).getId()) {
                    if (name.equals(list.get(i).getName())) {
                        list.remove(i);
                        System.out.println("删除成功");
                        return;
                    } else {
                        System.out.println("你输入的ID有误,请重新输入");
                    }
                } else {
                    System.out.println("你输入的ID有误,请重新输入");
                }
            }
        }
    }

    private static void lookDanBook(ArrayList<Book> list) {
        Scanner sc = new Scanner(System.in);
        int ID = sc.nextInt();
        for (int i = 0; i < list.size(); i++) {
            if(ID==list.get(i).getId()){
                System.out.println("ID"+"   "+"书名"+"   "+"作者"+"   "+"价格"+"   ");
                System.out.println(list.get(i).getId()+"   "+list.get(i).getName()+"   "+list.get(i).getAuthor()+"   "+list.get(i).getPrice());
            }
            if(i==list.size()-1&&ID!=list.get(i).getId()){
                System.out.println("查无此书 ");
            }
        }
    }

    private static void lookBook(ArrayList<Book> list) {
        for (int i = 0; i < list.size(); i++) {
            System.out.println("ID"+"   "+"书名"+"   "+"作者"+"   "+"价格"+"   ");
            System.out.println(list.get(i).getId()+"   "+list.get(i).getName()+"   "+list.get(i).getAuthor()+"   "+list.get(i).getPrice());
        }
    }

    private static void addBook(ArrayList<Book> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入书籍的Id");
        int Id;
     loo:   while(true) {
          Id = sc.nextInt();
         if(list.size()==0){
             break loo;
         }
            for (int i = 0; i <list.size(); i++) {
                if (Id == list.get(i).getId()) {
                    System.out.println("你输入的IＤ已存在请重新输入");
                }else{
                    break loo;
                }
            }
        }
        System.out.println("请输入书名");
        String name = sc.next();
        System.out.println("请输入作者名");
        String author = sc.next();
        System.out.println("请输入价格");
        double price = sc.nextDouble();
        Book bo = new Book(Id,name,author,price);
        list.add(bo);


    }

}
